package com.guodu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import com.guodu.dao.SupplierEvaluationDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.SupplierEvaluation;
import com.guodu.entity.SupplierEvaluationStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.SupplierEvaluationService;
import com.guodu.vo.SupplierEvaluationQueryVo;
import com.guodu.vo.SupplierEvaluationVo;

@Service("supplierEvaluationService")
public class SupplierEvaluationServiceImpl  implements SupplierEvaluationService{
	@Resource
	private SupplierEvaluationDao supplierEvaluationDao;
	@Resource
	private UnitsDao unitsDao;
	@Resource
	private UserInfoDao userInfoDao;

	
	public List<SupplierEvaluationVo> findByVo(
			SupplierEvaluationQueryVo supplierEvaluationQueryVo, Units units,
			int curPage, int pageSize) {
		List<SupplierEvaluationVo> supplierEvaluationVoList = new ArrayList<SupplierEvaluationVo>();
		List<SupplierEvaluation> supplierEvaluationList = supplierEvaluationDao.findByVo(supplierEvaluationQueryVo, units, curPage, pageSize);
		for(int i=0;i<supplierEvaluationList.size();i++){
			SupplierEvaluationVo supplierEvaluationVo = new SupplierEvaluationVo();
			supplierEvaluationVo.setSupplierEvaluation(supplierEvaluationList.get(i));
			UserInfo userInfo = userInfoDao.findById(supplierEvaluationList.get(i).getCreatePeople());	
			
			if(supplierEvaluationList.get(i).getUnitsId()!=null){
				Units unitsInfo = unitsDao.findById(Long.valueOf(supplierEvaluationList.get(i).getUnitsId()));
				if(unitsInfo.getUnitsLevel()==2L){
				supplierEvaluationVo.setUnitsName(unitsInfo.getUnitsName());
				supplierEvaluationVo.setUnitsId(unitsInfo.getUnitsId());
				supplierEvaluationVo.setIndustry(unitsInfo.getIndustry());
				supplierEvaluationVo.setUnitsId(unitsInfo.getUnitsId());
				Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
				supplierEvaluationVo.setFunitsName(funitsInfo.getUnitsName());
				supplierEvaluationVo.setFunitsId(unitsInfo.getFunitsId());
				}else{
					supplierEvaluationVo.setFunitsName(unitsInfo.getUnitsName());
					supplierEvaluationVo.setFunitsId(unitsInfo.getUnitsId());
				}
			}
			if(userInfo.getUnitsId() != null){
					Units createUnits =  unitsDao.findById(userInfo.getUnitsId());
					supplierEvaluationVo.setUnitsLevel(createUnits.getUnitsLevel());
			}else{
				supplierEvaluationVo.setJt("jt");
			}
			supplierEvaluationVo.setUserName(userInfo.getUserName());
			supplierEvaluationVoList.add(supplierEvaluationVo);			
		}	
		return supplierEvaluationVoList;
	}
	
	public List<SupplierEvaluation> downLoad(
			SupplierEvaluationQueryVo supplierEvaluationQueryVo,Units units) {
		
		return supplierEvaluationDao.downLoad(supplierEvaluationQueryVo,units);
	}
	@Override
	public void examinAll(SupplierEvaluationQueryVo supplierEvaluationQueryVo,
			Units units) {
		supplierEvaluationDao.examinAll(supplierEvaluationQueryVo,units);
		
	}
	
	public int findSizeByVo(SupplierEvaluationQueryVo supplierEvaluationQueryVo,Units units) {
		
		return supplierEvaluationDao.findSizeByVo(supplierEvaluationQueryVo,units);
	}

	public void save(SupplierEvaluation supplierEvaluation) {
		supplierEvaluationDao.save(supplierEvaluation);
		
	}
	public void examine(String ids,String reason,String beforeStatus,Long afterStatus,UserInfo userInfo) {
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<SupplierEvaluationStatus> supplierEvaluationStatusList = new ArrayList<SupplierEvaluationStatus>();
		for(int i=0;i<idses.length;i++){
			SupplierEvaluationStatus supplierEvaluationStatus = new SupplierEvaluationStatus();
			supplierEvaluationStatus.setAfterStatus(afterStatus);
			supplierEvaluationStatus.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			supplierEvaluationStatus.setCreatePeople(userInfo.getUserId());
			supplierEvaluationStatus.setSupplierId(idses[i]);
			supplierEvaluationStatus.setReason(reason);
			supplierEvaluationStatus.setCreateTime(Calendar.getInstance().getTime());
			supplierEvaluationStatus.setStatusId(i+"");
			supplierEvaluationStatusList.add(supplierEvaluationStatus);
		}		
		supplierEvaluationDao.changeStatus(supplierEvaluationStatusList);
		supplierEvaluationDao.examine(ids, afterStatus);
		
	}

	public void delByIds(String[] ids) {
		supplierEvaluationDao.delByIds(ids);
		
	}

	public SupplierEvaluation findById(String id) {
		
		return supplierEvaluationDao.findById(id);
	}

	public void update(SupplierEvaluation supplierEvaluation,Long userId) {
		if(supplierEvaluation.getStatus() == 4L){
			List<SupplierEvaluationStatus> supplierEvaluationStatusList = new ArrayList<SupplierEvaluationStatus>();
			SupplierEvaluationStatus supplierEvaluationStatus = new SupplierEvaluationStatus();
			supplierEvaluationStatus.setAfterStatus(0L);
			supplierEvaluationStatus.setBeforeStatus(supplierEvaluation.getStatus());
			supplierEvaluationStatus.setCreatePeople(userId);
			supplierEvaluationStatus.setSupplierId(supplierEvaluation.getSupplierId());
			supplierEvaluationStatus.setReason("");
			supplierEvaluationStatus.setCreateTime(Calendar.getInstance().getTime());
			supplierEvaluationStatus.setStatusId("1");
			supplierEvaluationStatusList.add(supplierEvaluationStatus);
			supplierEvaluationDao.changeStatus(supplierEvaluationStatusList);
		}		
		supplierEvaluation.setStatus(0L);
//		Units units = (Units)ServletActionContext.getContext().getSession().get("unitsbean");
//		if(units == null){
//			supplierEvaluation.setStatus(3L);		
//		}
		supplierEvaluationDao.update(supplierEvaluation);
		
	}

	public List<SupplierEvaluationStatus> findStatus(String id) {
		
		return supplierEvaluationDao.findStatus(id);
	}

	public void deleteById(String id) {
		
		supplierEvaluationDao.deleteById(id);
	}




	


	
	
	
	
}
